package org.ix.gateway.route.controller;

import org.ix.common.annotation.SetFieldTypes;
import org.ix.common.models.PageList;
import org.ix.common.utils.ExcelUtils;
import org.ix.gateway.route.model.request.GatewayRouteAddRequest;
import org.ix.gateway.route.model.request.GatewayRoutePatchRequest;
import org.ix.gateway.route.model.request.GatewayRouteQueryRequest;
import org.ix.gateway.route.model.request.GatewayRouteSaveRequest;
import org.ix.gateway.route.model.response.GatewayRouteDetailResponse;
import org.ix.gateway.route.model.response.GatewayRouteListItemResponse;
import org.ix.gateway.route.service.GatewayRouteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.*;
import java.io.IOException;
import java.net.URI;
import java.util.List;

@Api(tags = {"网关路由相关接口"})
@CrossOrigin
@RequestMapping("/gateway-route")
@RestController
@Validated
public class GatewayRouteController {

    private final GatewayRouteService gatewayRouteService;

    @Autowired
    public GatewayRouteController(GatewayRouteService gatewayRouteService) {
        this.gatewayRouteService = gatewayRouteService;
    }

    @ApiOperation(value = "添加网关路由", notes = "添加网关路由")
    @ApiImplicitParam(name = "gatewayRoute", value = "添加网关路由请求实体", required = true, dataType = "GatewayRouteAddRequest", paramType = "body")
    @PostMapping("/add")
    public ResponseEntity<GatewayRouteDetailResponse> add(@Validated @RequestBody @SetFieldTypes(types = {"create"}) GatewayRouteAddRequest gatewayRoute) {
        GatewayRouteDetailResponse detail = gatewayRouteService.add(gatewayRoute);
        return ResponseEntity.created(URI.create("")).body(detail);
    }

    @ApiOperation(value = "删除网关路由", notes = "删除网关路由")
    @DeleteMapping("/delete")
    public ResponseEntity<Integer> delete(@Validated @SetFieldTypes(types = {"update"}) GatewayRoutePatchRequest patchRequest) {
        Integer count = gatewayRouteService.delete(patchRequest);
        return ResponseEntity.ok(count);
    }

    @ApiOperation(value = "禁用网关路由", notes = "禁用网关路由")
    @PatchMapping("/disable")
    public ResponseEntity<Integer> disable(@Validated @SetFieldTypes(types = {"update"}) GatewayRoutePatchRequest patchRequest) {
        Integer count = gatewayRouteService.disable(patchRequest);
        return ResponseEntity.ok(count);
    }

    @ApiOperation(value = "启用网关路由", notes = "启用网关路由")
    @PatchMapping("/enable")
    public ResponseEntity<Integer> enable(@Validated @SetFieldTypes(types = {"update"}) GatewayRoutePatchRequest patchRequest) {
        Integer count = gatewayRouteService.enable(patchRequest);
        return ResponseEntity.ok(count);
    }

    @ApiOperation(value = "根据路由id更新网关路由", notes = "根据路由id更新网关路由")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "gatewayRoute", value = "更新网关路由请求实体", required = true, dataType = "GatewayRouteSaveRequest", paramType = "body"),
            @ApiImplicitParam(name = "routeId", value = "路由id", required = true, dataType = "String", paramType = "query")
    })
    @PatchMapping("/update")
    public ResponseEntity<GatewayRouteDetailResponse> updateByRouteId(
            @Validated @RequestBody @SetFieldTypes(types = {"update"}) GatewayRouteSaveRequest gatewayRoute,
            @RequestParam("routeId") @NotBlank(message = "routeId(路由id)不能为空") String routeId
    ) {
        GatewayRouteDetailResponse detail = gatewayRouteService.updateById(gatewayRoute, routeId);
        return ResponseEntity.ok(detail);
    }

    @ApiOperation(value = "根据路由id获取网关路由详情", notes = "根据路由id获取网关路由详情")
    @ApiImplicitParam(name = "routeId", value = "路由id", required = true, dataType = "String", paramType = "query")
    @GetMapping("/detail")
    public ResponseEntity<GatewayRouteDetailResponse> getByRouteId(@RequestParam("routeId") @NotBlank(message = "路由id不能为空") String routeId) {
        GatewayRouteDetailResponse detail = gatewayRouteService.getById(routeId);
        return ResponseEntity.ok(detail);
    }

    @ApiOperation(value = "根据条件获取网关路由列表", notes = "根据条件获取网关路由列表")
    @GetMapping("/list")
    public ResponseEntity<List<GatewayRouteListItemResponse>> getList(GatewayRouteQueryRequest queryRequest) {
        return ResponseEntity.ok(gatewayRouteService.getList(queryRequest));
    }

    @ApiOperation(value = "根据条件分页获取网关路由列表", notes = "根据条件分页获取网关路由列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页码", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", dataType = "int", paramType = "query")
    })
    @GetMapping("/page-list")
    public ResponseEntity<PageList<GatewayRouteListItemResponse>> getPageList(
            GatewayRouteQueryRequest queryRequest,
            @RequestParam(value = "pageNum", defaultValue = "1") @Min(value = 1, message = "页码必须大于0") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "50") @Min(value = 1, message = "分页大小必须大于0") Integer pageSize
    ) {
        return ResponseEntity.ok(gatewayRouteService.getPageList(queryRequest, pageNum, pageSize));
    }

    @ApiOperation(value = "根据条件导出网关路由列表", notes = "根据条件导出网关路由列表")
    @ApiImplicitParam(name = "exportName", value = "导出文件名", dataType = "String", paramType = "query")
    @GetMapping("/export")
    public ResponseEntity<?> export(HttpServletResponse response, GatewayRouteQueryRequest queryRequest,
                                    @RequestParam(value = "exportName", defaultValue = "网关路由", required = false) String exportName) throws IOException {

        ExcelUtils.export(response, gatewayRouteService.getList(queryRequest), GatewayRouteListItemResponse.class, exportName, "网关路由");
        return null;
    }
}
